/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using QName = System.Qizx.Api.QName;
using QizxException = System.Qizx.Api.QizxException;
using EvalContext = System.Qizx.XQuery.EvalContext;
using ExprDisplay = System.Qizx.XQuery.ExprDisplay;
using Focus = System.Qizx.XQuery.Focus;
using ModuleContext = System.Qizx.XQuery.ModuleContext;
using XQValue = System.Qizx.XQuery.XQValue;
using Function = System.Qizx.XQuery.Fn.Function;
namespace System.Qizx.XQuery.Op
{
	
	
	/// <summary> Call to Function: temporarily built by the parser. After static check, it is
	/// replaced by predefined function runtime, (XXXfunction.RT) UserFunction.Call,
	/// JavaFunction.Call etc
	/// </summary>
	public class FunctionCall:Expression
	{
		virtual public int ArgCount
		{
			get
			{
				return args.Length;
			}
			
		}
		public QName name;
		
		internal Expression[] args = new Expression[0];
		
		public FunctionCall(QName name)
		{
			this.name = name;
		}
		
		public virtual void  addArgument(Expression arg)
		{
			args = addExpr(args, arg);
		}
		
		public override Expression Child(int rank)
		{
			return (rank < args.Length)?args[rank]:null;
		}
		
		public override void  Dump(ExprDisplay d)
		{
			d.header(this);
			d.property("name", name);
		}
		
		public override Expression StaticCheck(ModuleContext context, int flags)
		{
			// TODO special functions may want to check their arguments themselves
			
			for (int a = 0; a < args.Length; a++)
				args[a] = context.staticCheck(args[a], 0);
			// lookup simply by name: overloaded functions hold several prototypes
			// selected by Function.staticCheck(context, arg)
			// Note: staticCheck happens after all functions declarations
			// have been processed
			try
			{
				Function fun = context.functionLookup(name);
				if (fun == null)
				{
					context.error("XPST0017", this, "unknown function '" + context.PrefixedName(name) + "'");
					return this;
				}
				return fun.staticCheck(context, args, this);
			}
			catch (QizxException e)
			{
				context.error(e.getErrorCode(), this, "error on function '" + context.PrefixedName(name) + "': " + e.Message);
				return this;
			}
		}
		
		public override XQValue eval(Focus focus, EvalContext context)
		{
			throw new System.SystemException("BUG: FunctionCall.eval(" + name + ") : should not happen");
		}
	}
}